import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtMerchantApplyApi } from '#/api/cmt/base/merchantApply';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getRangePickerDefaultProps } from '#/utils';

/** 商户类型选项 */
export const MERCHANT_TYPE_OPTIONS = [
  { label: '企业', value: 1 },
  { label: '机构', value: 2 },
  { label: '社会组织', value: 3 },
  { label: '个体', value: 4 },
];

/** 审核状态选项 */
export const AUDIT_STATUS_OPTIONS = [
  { label: '待审核', value: 0 },
  { label: '通过', value: 1 },
  { label: '拒绝', value: 2 },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'name',
      label: '商户名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入商户名称',
      },
    },
    {
      fieldName: 'merchantType',
      label: '商户类型',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: MERCHANT_TYPE_OPTIONS,
        placeholder: '请选择商户类型',
      },
    },
    {
      fieldName: 'legalPerson',
      label: '法人代表',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入法人代表',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'licenseNo',
      label: '营业执照号',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入营业执照号',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: z.number().default(CommonStatusEnum.ENABLE).optional(),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        clearable: true,
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'name',
      label: '商户名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入商户名称',
      },
    },
    {
      fieldName: 'merchantType',
      label: '商户类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: MERCHANT_TYPE_OPTIONS,
        placeholder: '请选择商户类型',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtMerchantApplyApi.MerchantApply>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '申请ID',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '商户名称',
      minWidth: 150,
    },
    {
      field: 'merchantType',
      title: '商户类型',
      minWidth: 100,
      slots: { default: 'merchantType' },
    },
    {
      field: 'legalPerson',
      title: '法人代表',
      minWidth: 100,
    },
    {
      field: 'contactPerson',
      title: '联系人',
      minWidth: 100,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'licenseNo',
      title: '营业执照号',
      minWidth: 150,
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'auditTime',
      title: '审核时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'auditRemark',
      title: '审核备注',
      minWidth: 150,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
